package com.whmskj.xjlhbc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whmskj.xjlhbc.domain.DetailedProductionPlanForSc11;
import com.whmskj.xjlhbc.domain.Statistics;
import com.whmskj.xjlhbc.domain.SummaryTableOfProductionPlanSc10;
import com.whmskj.xjlhbc.dto.ScPlanDto;
import com.whmskj.xjlhbc.mapper.DetailedProductionPlanForSc11Mapper;
import com.whmskj.xjlhbc.mapper.SummaryTableOfProductionPlanSc10Mapper;
import com.whmskj.xjlhbc.service.IPlanService;
import com.whmskj.xjlhbc.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PlanServiceImpl implements IPlanService {

    @Autowired
    private SummaryTableOfProductionPlanSc10Mapper summaryTableOfProductionPlanSc10Mapper;

    @Autowired
    private DetailedProductionPlanForSc11Mapper detailedProductionPlanForSc11Mapper;

    @Autowired
    private IStatisticsService statisticsService;

    @Override
    public ScPlanDto getScPlan(String date) {
        date = StringUtils.hasText(date) ? date : LocalDate.now().toString();
        ScPlanDto result = new ScPlanDto();

        LambdaQueryWrapper<SummaryTableOfProductionPlanSc10> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SummaryTableOfProductionPlanSc10::getPlannedDate, date).last("limit 1");
        SummaryTableOfProductionPlanSc10 summaryTableOfProductionPlanSc10 = Optional.ofNullable(summaryTableOfProductionPlanSc10Mapper.selectOne(wrapper)).orElseGet(SummaryTableOfProductionPlanSc10::new);
        result.setJs1(summaryTableOfProductionPlanSc10.getLavaNo1());
        result.setJs2(summaryTableOfProductionPlanSc10.getMask2());
        result.setGzList(StringUtils.hasText(summaryTableOfProductionPlanSc10.getWeaving()) ? Arrays.asList(summaryTableOfProductionPlanSc10.getWeaving().split("，")) : new ArrayList<>());
        List<String> yjbList = result.getYjbList();
        yjbList.add(summaryTableOfProductionPlanSc10.getDemandForRawMaterials());
        yjbList.add(summaryTableOfProductionPlanSc10.getSparePartsRequirements1());
        yjbList.add(summaryTableOfProductionPlanSc10.getPulpRequirements());
        LambdaQueryWrapper<DetailedProductionPlanForSc11> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DetailedProductionPlanForSc11::getPlannedDate, date);

        List<DetailedProductionPlanForSc11> detailedProductionPlanForSc11s = detailedProductionPlanForSc11Mapper.selectList(wrapper1);
        result.setPlanList(detailedProductionPlanForSc11s.stream().map(item -> {
            ScPlanDto.PlanList plan = new ScPlanDto.PlanList();
            plan.setPh(item.getNoNo1());
            plan.setPm(item.getGoods());
            plan.setStartAt190(item.getStartAt190());
            plan.setOpeningNumber230(item.getOpeningNumber230());
            plan.setAxes190(item.getAxes190());
            plan.setAxes230(item.getAxes230());
            plan.setAxle190(item.getAxle190());
            plan.setAxle230(item.getAxle230());
            plan.setKh(item.getF67a());
            plan.setTf(item.getF0ed());
            plan.setBz(item.getRemarks());
            plan.setAmNight5(item.getIts190Am() + "+" + item.getIts190());
            plan.setAmNight230(item.getIts230Am() + "+" + item.getIts230());
            return plan;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public List<Object> getZjPlan(String date) {
        LocalDate ld = StringUtils.hasText(date) ? LocalDate.parse(date) : LocalDate.now();
        LocalDate ld1 = ld.minusDays(1);
        QueryWrapper<Statistics> wrapper = new QueryWrapper<>();
        wrapper.in("dateymd",ld.toString(),ld1.toString());
        wrapper.select("dateymd","macid","speed","variety");
        wrapper.last("group by dateymd,macid,speed,variety order by macid+0");
        List<Statistics> statisticsList = statisticsService.list(wrapper);
        List<Statistics> list = statisticsList.stream().filter(f -> f.getDateymd().equals(ld.toString())).collect(Collectors.toList());

        return list.stream().map(item -> {
            Statistics statistics = statisticsList.stream().filter(f -> f.getDateymd().equals(ld1.toString()) && f.getMacid().equals(item.getMacid())).collect(Collectors.toList()).stream().findFirst().orElseGet(Statistics::new);
            Map<String, String> map = new HashMap<>();
            map.put("macid", item.getMacid());
            map.put("speed", item.getSpeed());
            map.put("variety", item.getVariety());
            map.put("variety1", statistics.getVariety());
            return map;
        }).collect(Collectors.toList());
    }
}
